package com.hndzyu.yd.vip;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Base64;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.hndzyu.yd.R;
import com.hndzyu.yd.bean.GameList;
import com.hndzyu.yd.bean.UserBean;
import com.hndzyu.yd.url.Path;
import com.hndzyu.yd.utils.OkHttpDownloadJsonUtil;
import com.hndzyu.yd.utils.Util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zz9527 on 2018/12/5.
 */
public class TJFragment extends Fragment {
    private String encode;
    private ListView lv;
    private int page = 1;
    private List<GameList.DataBean.ListBean> list;
//    @Override
//    public void onActivityCreated(Bundle savedInstanceState) {
//        super.onActivityCreated(savedInstanceState);
//        if (savedInstanceState != null) {
//            //读取保存的值
//            UserBean.uid = savedInstanceState.getInt("uid", 0);
//            Log.i("_________","读取保存"+UserBean.uid);
//        }
//    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_tj, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        SmartRefreshLayout refresh = view.findViewById(R.id.refresh);
        refresh.setRefreshHeader(new MaterialHeader(getContext()));
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
                list.clear();
                page = 1;
                getData();
            }
        });
        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                refreshLayout.finishLoadMore(2000/*,false*/);//传入false表示刷新失败
                page++;
                getData();
            }
        });
        lv = view.findViewById(R.id.lv_game);
        String str = Util.getIMEI(getContext()) + "#" + UserBean.uid + "#231#abcf1667d65e97a0d67b0b6dde5ad486";
        String base = Base64.encodeToString(str.getBytes(), Base64.NO_WRAP).trim();
        encode = encode(base);
        list = new ArrayList<>();
        getData();
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                if (UserBean.level == 0) {
//                    final VipDIalog vipDIalog = new VipDIalog(getContext());
//                    vipDIalog.setVIPListener(new VipDIalog.VIPListener() {
//                        @Override
//                        public void chooseTrue() {
//                            startActivity(new Intent(getContext(), PayVIPActivity.class));
//                            vipDIalog.dismiss();
//                        }
//
//                        @Override
//                        public void chooseFalse() {
//                            vipDIalog.dismiss();
//
//                        }
//                    });
//                    vipDIalog.show();
//                    vipDIalog.setContent();
//                } else {
                    Intent intent = new Intent(getContext(), GameDetailActivity.class);
                    intent.putExtra("sign", encode);
                    intent.putExtra("idtask", list.get(i).getIDTask());
                    intent.putStringArrayListExtra("bq", (ArrayList<String>) list.get(i).getLabel());
                    startActivity(intent);
//                }
            }
        });
    }

//    @Override
//    public void onSaveInstanceState(Bundle outState) {
//        super.onSaveInstanceState(outState);
//        outState.putInt("uid", UserBean.uid);
//        Log.i("_________","回收前保存");
//    }
    private void getData() {


            OkHttpDownloadJsonUtil.downloadJson(getContext(), Path.GAMELIST(Util.getIMEI(getContext()), UserBean.uid, encode, page), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {

                @Override
                public void onsendJson(String json) {
                    try {
                        Gson gson = new Gson();
                        List<GameList.DataBean.ListBean> mlist = gson.fromJson(json, GameList.class).getData().getList();
                        if (mlist.size() == 0) {
                            Toast.makeText(getContext(), "没有更多了", Toast.LENGTH_SHORT).show();
                            return;
                        } else {
                            list.addAll(mlist);
                        }
                        lv.setAdapter(new GameListAdapter(getContext(), list));
                    } catch (Exception e) {
                    }
                }
            });


    }

    /**
     * md5 32 小写
     *
     * @param text
     * @return
     */
    public static String encode(String text) {
        try {
            MessageDigest digest = MessageDigest.getInstance("md5");
            byte[] result = digest.digest(text.getBytes());
            StringBuilder sb = new StringBuilder();
            for (byte b : result) {
                int number = b & 0xff;
                String hex = Integer.toHexString(number);
                if (hex.length() == 1) {
                    sb.append("0" + hex);
                } else {
                    sb.append(hex);
                }
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        return "";
    }
}
